<?php
if(!function_exists('order_status')){
    function order_status($order){
        $status_msg = '';
        // 重写订单状态-
        // if($order['order_status'] == -1){
        //     $status_msg  = '已关闭交易'; 
        // }

        // if($order['order_status'] == 0){
        //     $status_msg  = '已取消交易'; 
        // }

        // if($order['order_status'] == 1){
        //     if($order['pay_status'] == 0){
        //         $status_msg  = '待付款';
        //     }
            
        //     if($order['pay_status'] == 1){
        //         if($order['shipping_status'] == 0){
        //             $status_msg  = '待发货';    
        //         }
        //         if($order['shipping_status'] == 1){
        //             $status_msg  = '待收货'; 
        //         }
        //     }
        // }
        // if($order['order_status'] == 2){
        //     $status_msg  = '已完成'; 
        // }
        if(isset($order['del_status']) && $order['del_status'] == 1){
            $status_msg  = '已删除'; 
        }
        // 新版
        switch ($order['order_status']) {
            case '1':// 创建状态
                $status_msg = '待确定';
                break;
            case '2':
                $status_msg = '已确认';
                break;
            default:
                break;
        }
        return $status_msg;
    }
}
if(!function_exists('order_btn')){
    function order_btn($order){
        $order_btn = [];
        // 重写订单状态-
        // if($order['order_status'] == -1){
        //     $order_btn['jy_colse']  = 1; 
        // }

        // if($order['order_status'] == 0){
        //     $order_btn['jy_cancel']  = 1; 
        // }

        // if($order['order_status'] == 1){
        //     if($order['pay_status'] == 0){
        //         $order_btn['colse_order']  = 1;
        //     }
            
        //     if($order['pay_status'] == 1){
        //         if($order['shipping_status'] == 0){
        //             $order_btn['to_shipping']  = 1;
        //         }
        //         if($order['shipping_status'] == 1){
        //             $order_btn['wait_sh']  = 1;
        //         }
        //     }
        // }
        // if($order['order_status'] == 2){
        //     $order_btn['complete']  = 1;
        // }
        // 新版
        switch ($order['order_status']) {
            case '1':// 创建状态
                $order_btn['dqr_btn']  = 1;// 待确认
                break;
            case '2':
                $order_btn['yqr_btn']  = 1;// 已确认
                break;
            default:
                break;
        }
        return $order_btn;
    }
}

if(!function_exists('return_pay')){
    function return_pay($return_id)
    {
        $return = M('return')->where(['id'=>$return_id])->find();
        if(empty($return)){
            return ['status'=>0, 'msg'=>'退款单不存在'];
        }
        if($return['status']!=5){
            return ['status'=>0, 'msg'=>'退款单状态不符合退款'];
        }
        $order = M('order')->where(['id'=>$return['order_id']])->find();
        if(empty($order)){
            return ['status'=>0, 'msg'=>'订单未查询到'];
        }
        Vendor("riverslei.payment.autoload");
        $model = M('return_pay');
        $refund_no = 'RP'.date('YmdHis').rand(10000, 99999);
        $refund_data = [
            'refund_no'     => $refund_no,
            'refund_fee'    => $return['money'],
            'refund_method' => $order['pay_method'],
            'out_trade_no'  => $order['code'],
            'reason'        => $return['reason'],
            'status'        => 1,
            'msg'           => '',
            'create_time'   => time(),
            'update_time'   => time(),
        ];

        switch ($order['pay_method']) {
            case 'ali_app':
                $type = \Payment\Config::ALI_REFUND;
                $data = [
                    'out_trade_no'  => $order['code'],
                    'refund_fee'    => $return['money'],
                    'reason'        => '退款',
                    'refund_no'     => $refund_no,
                ];
                $action = 'ali';
                break;
            case 'wx_app':
                $type = \Payment\Config::WX_REFUND;
                $data = [
                    'out_trade_no'  => $order['code'],
                    'total_fee'     => $order['order'],
                    'refund_fee'    => $return['money'],
                    'refund_no'     => $refund_no,
                    'refund_account' => \Payment\Common\WxConfig::REFUND_RECHARGE,// REFUND_RECHARGE:可用余额退款  REFUND_UNSETTLED:未结算资金退款（默认）
                ];
                $action = 'wechat';
                break;
            default:
                return ['status'=>0, 'msg'=>'退款方式不存在'];
                break;
        }
        $pay = new \Common\Util\Pay();
        
        $pay_refund_id = $model->add($refund_data);

        $result = $pay->$action()->run($type, $data);
        trace($result, 'RETURN_PAY');
        if($result && $result['is_success'] == 'T'){
            // 退款成功
            $model->where(['id'=>$pay_refund_id])->save(['status'=>2, 'msg'=>'ok']);
            M('order')->where(['id'=>$order['id']])->setInc('return_money', $return['money']);
            return ['status'=>1, 'msg'=>'ok'];
        }else{
            // 退款失败
            $update = [
                'status'    => '3',
                'msg'       => isset($result['error']) ? $result['error'] : '',
            ];
            $model->where(['id'=>$pay_refund_id])->save($update);
            return ['status'=>0, 'msg'=>$result['error']];
        }
    }
}